package com.kzd.gtboon.main

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.*
import com.kzd.gtboon.R
import com.kzd.gtboon.app.SPSConstance.uid
import com.kzd.gtboon.app.SPSConstance.userInfo
import com.kzd.gtboon.app.SPSConstance.userMine
import com.kzd.gtboon.custom.setOnClickAntiShakeListener
import com.kzd.gtboon.custom.setTransitionStatus
import com.kzd.gtboon.databinding.FragmentMainMineBinding
import com.kzd.gtboon.entity.UserInfo
import com.kzd.gtboon.entity.UserMine
import com.kzd.gtboon.jetpack.vm.MineViewModel
import com.kzd.gtboon.main.mine.*
import com.kzd.gtboon.main.popup.AppUpDatePopup
import com.kzd.gtboon.main.popup.ProgressPopup
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel
import java.lang.StringBuilder

class MainMineFragment : OwnerViewBindingFragment<FragmentMainMineBinding>() {

    companion object {
        fun newInstance(): MainMineFragment {
            return MainMineFragment()
        }
    }

    private val mProgressPopup: ProgressPopup by lazy {
        ProgressPopup(context = requireContext(), onCancel = {}, onConfirm = {})
    }

    private val mAppUpDatePopup: AppUpDatePopup by lazy {
        AppUpDatePopup(context = requireContext(), onClick = {
            mAppUpDatePopup.dismiss()
            val fileUrl = mViewModel.userAppUpdateLiveData.value
                ?.data?.data?.apk_file_url ?: return@AppUpDatePopup
            mViewModel.userDownload(fileUrl) {
                mProgressPopup.setProgress(it)
            }
        })
    }

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMainMineBinding
        get() = FragmentMainMineBinding::inflate

    override fun initView() {

        //用户中心
        mViewBinding.clMainMineUserInfo.setOnClickAntiShakeListener {
            toMineUserActivity()
        }

        //收藏中心
        mViewBinding.tvMainMineCoupon.setOnClickAntiShakeListener {
            toMineCouponActivity()
        }

        //我的礼包
        mViewBinding.tvMainMineGiving.setOnClickAntiShakeListener {
            toMineGiftActivity()
        }

        //免责声明
        mViewBinding.tvMineSettingState.setOnClickAntiShakeListener {
            toMineRichTextActivityApi(
                RichTextApi.AGREEMENT_MZSM.title,
                RichTextApi.AGREEMENT_MZSM.type
            )
        }

        //用户协议
        mViewBinding.tvMineSettingUserAgree.setOnClickAntiShakeListener {
            toMineRichTextActivityApi(
                RichTextApi.AGREEMENT_YHXY.title,
                RichTextApi.AGREEMENT_YHXY.type
            )
        }

        //隐私政策
        mViewBinding.tvMineSettingPrivacy.setOnClickAntiShakeListener {
            toMineRichTextActivityApi(
                RichTextApi.AGREEMENT_YSZC.title,
                RichTextApi.AGREEMENT_YSZC.type
            )
        }

        //检查更新
        mViewBinding.flMineSettingUpdate.setOnClickAntiShakeListener {
            mViewModel.userAppUpDate(id = 2)
        }

        mViewBinding.tvMineSettingVersionName.text = StringBuilder().append("V")
            .append(AppUtils.getAppVersionName())

    }


    override fun initData() {
        mViewModel.userAppTips()
    }

    override fun onResume() {
        super.onResume()
        fillData(userInfo, userMine)
        mViewModel.userInfo()
        mViewModel.userMine()
    }

    override fun initViewMode() {
        mViewModel.userInfoLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data, userMine)
        })
        mViewModel.userMineLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(userInfo, data)
        })

        mViewModel.userAppUpdateLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mAppUpDatePopup.setContent(data.update_log)
                .showPopupWindow()
        })

        mViewModel.userDownloadLiveData.observe(this as LifecycleOwner, { response ->
            when (response.status) {
                Status.START -> {
                    mProgressPopup.resetProgress()
                    mProgressPopup.showPopupWindow()
                }
                Status.FAILURE -> {
                    mProgressPopup.dismiss()
                    toast(response.error?.msg)
                }
                Status.SUCCESS -> {
                    mProgressPopup.dismiss()
                    val data = response.data?.data
                        ?: return@observe
                    AppUtils.installApp(data)
                }
            }
        })
    }

    private fun fillData(userInfo: UserInfo?, userMine: UserMine?) {

        mViewBinding.ivMainMineUserHeader.loadCircleImage(
            if (userInfo?.face.isNullOrEmpty())
                R.mipmap.ic_test_1 else userInfo?.face ?: R.mipmap.ic_test_1
        )

        if (uid.isNullOrEmpty()) {
            mViewBinding.tvMainMineUserPhone.visibility = View.GONE
        } else {
            mViewBinding.tvMainMineUserPhone.visibility = View.VISIBLE
        }

        mViewBinding.tvMainMineUserNickname.text = when {
            uid.isNullOrEmpty() -> "未登录"
            userInfo?.pet_name.isNullOrEmpty() -> userInfo?.user_name
            else -> userInfo?.pet_name
        }

        mViewBinding.tvMainMineUserPhone.text =
            if (!userInfo?.mobile.isNullOrEmpty()) userInfo?.mobile else "未绑定"
    }
}